package com.zhf.demo03.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/14 20:26
 */
@WebServlet(name = "SessionServlet2",urlPatterns = "/session2")
public class SessionServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置属性名和属性值
        HttpSession session = request.getSession();
        session.setAttribute("name","Mr.Zhao");
        //获取属性名对应的属性值
        Object name = session.getAttribute("name");
        System.out.println("获取到的属性值为："+name);
        //删除属性名对应的属性值
        session.removeAttribute("name");
        //获取属性名对应的属性值
        System.out.println("获取到的属性值为："+session.getAttribute("name"));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
